package skills.jssvc.deu.cn.skillsecond;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import skills.jssvc.deu.cn.skillsecond.programming1.MyAccount2;
import skills.jssvc.deu.cn.skillsecond.programming1.MyAccount3;
import skills.jssvc.deu.cn.skillsecond.programming1.MyAccount_Fragment_1;
import skills.jssvc.deu.cn.skillsecond.programming2.RedMangFragment;
import skills.jssvc.deu.cn.skillsecond.programming3.BillMangFragment;
import skills.jssvc.deu.cn.skillsecond.programming5.EnvironmentalFragment;
import skills.jssvc.deu.cn.skillsecond.programming6.RealDisplayFragment;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity";

    private DrawerLayout drawerLayout;
    private TextView homeTv;
    private ListView actionList;
    private ArrayList<HashMap<String,Object>> itemList;
    private SimpleAdapter adapter;
    private String[] action_name;
    private int[] action_iocn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListens();
    }

    private void initView(){
        drawerLayout = (DrawerLayout) findViewById(R.id.main_drawerlayout);
        homeTv = (TextView) findViewById(R.id.main_home);
        actionList = (ListView) findViewById(R.id.main_action_list);
        initData();
    }

    private void initData(){
        final String[] actionTexts = new String[]{
                "模块1",
                "模块2",
                "模块3",
                "模块4",
                "模块5",
                "模块6",
                "模块7",
                "模块8",
                "模块9",
                "模块10",
        };
        int[] actionImages = new int[]{
                R.drawable.btn_l_book,
                R.drawable.btn_l_book,
                R.drawable.btn_l_book,
                R.drawable.btn_l_book,
                R.drawable.btn_l_book,
                R.drawable.btn_l_grid,
                R.drawable.btn_l_grid,
                R.drawable.btn_l_grid,
                R.drawable.btn_l_grid,
                R.drawable.btn_l_grid
        };
        itemList = new ArrayList<>();
        adapter = new SimpleAdapter(getApplicationContext(),itemList,R.layout.main_left_item,
                new String[]{"action_icon","action_name"},
                new int[]{R.id.main_action_icon,R.id.main_action_name});
        for (int i = 0; i < actionImages.length; ++i) {
            HashMap<String, Object> item1 = new HashMap<>();
            item1.put("action_icon", actionImages[i]);
            item1.put("action_name", actionTexts[i]);
            itemList.add(item1);
        }
        actionList.setAdapter(adapter);
    }

    private void setListens(){
        homeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.START);
            }
        });

        actionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                drawerLayout.closeDrawers();
                switch (i){
                    case 0:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,new MyAccount3()).commit();
                        break;
                    case 1:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,new RedMangFragment()).commit();
                        break;
                    case 2:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,new BillMangFragment()).commit();
                        break;
                    case 3:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,new RealDisplayFragment()).commit();
                        break;
                    case 4:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,new EnvironmentalFragment()).commit();
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                    case 8:
                        break;
                    case 9:
                        break;
                }
                Log.d(TAG, "onItemClick: i="+i+"    l="+l);
            }
        });

    }



}
